<?php

namespace core\entity;

/**
 * Truy vấn
 *
 * @author BapCai
 */
class Query {

    public $cri;
    public $page;
    public $sort;

    public function __construct(Criteria $cri = null) {
        $this->cri = $cri;
    }

    public function query(Criteria $cri) {
        $this->cri = $cri;
        return $this;
    }

    public function withPage(Page $page) {
        $this->page = $page;
        return $this;
    }

    public function withSort(Sort $sort) {
        $this->sort = $sort;
        return $this;
    }

    public function getQuery() {
        $where = '';
        if ($this->cri != null) {
            $where .= $this->cri;
        }
        if ($where == '') {
            $where .= '1=1';
        }
        if ($this->sort != null) {
            $where .= ' ORDER BY ';
            $where .= $this->sort;
        }
        if ($this->page != null && is_object($this->page)) {
            $where .= ' LIMIT ';
            $where .= $this->page;
        }
        return $where;
    }

    public function __toString() {
        return $this->getQuery();
    }

}
